Skip to content

fix(supervisor): terminate an exec's process group when its channel closes - #24

Merged
link108 merged 1 commit into
mainfrom
kindo/kill-exec-on-channel-close
Oct 14, 2026
Merged

link108 merged 1 commit into
mainfrom
kindo/kill-exec-on-channel-close

Conversation

@link108

@link108 link108 commented Oct 14, 2026

Copy link
Copy Markdown

Summary

When the gateway's exec deadline (timeout_seconds) expires, or a caller cancels an exec stream, the gateway sends exit 124 and closes the SSH channel, but the supervisor never stops the child. The command keeps running until it exits on its own or the pod goes away. This PR terminates the exec's process group when its channel or SSH connection closes before the command has exited.

Related Issue

No upstream issue. Kindo needs this so that exec deadlines actually bound execution in sandboxes. Downstream discussion: sandbox-api exec deadlines (UsableMachines/mono#14836).

Changes

  • Process groups: pipe execs, including sftp, now run as the leader of their own process group (process_group(0); std calls setpgid before the existing pre_exec hooks). PTY execs already call setsid.
  • New ExecProcessGroup (exec_group.rs): on channel close (channel_close) or connection drop (Drop for SshHandler) before the leader exits, it sends SIGTERM to the group. A background thread then waits up to EXEC_TERMINATION_GRACE (5s) and sends SIGKILL if any member is still running. On Linux it checks /proc for group members; elsewhere it falls back to whether the leader is alive. The async handler never blocks.
  • No signals to a recycled PID:
    • The exit waiter detects exit with waitid(EXITED | NOWAIT) and records it under the same lock that signals are sent under; only then does it reap.
    • While a termination is running, reaping waits, so the group ID stays reserved through the SIGKILL step.
    • The leader is in managed_children, so the orphan reaper never reaps it first.
  • Normal exits: the normal exit path is unchanged. A peer's close after the command exited sends no signal. Canonical main-session attachments never get an exec group.
  • Logging: OCSF ProcessActivity events for the SIGTERM, the SIGKILL escalation, and any killpg failure other than ESRCH.
  • Docs: one paragraph in architecture/sandbox.md.

Known limits:

  • Processes that create their own session or group (daemons, setsid) are not killed.
  • On non-Linux hosts, a grandchild that ignores SIGTERM survives once the leader exits, because there is no /proc.

Testing

  • cargo fmt --all -- --check passes.
  • cargo clippy -p openshell-supervisor-process --all-targets -- -D warnings is clean on macOS and Linux.
  • cargo test -p openshell-supervisor-process passes: 169 passed on macOS, and 271 passed on Linux (rust:1.95-bookworm container).
  • The new exec_group tests passed 25 of 25 repeated runs on Linux. They cover:
    • SIGTERM stops the leader and a grandchild.
    • Escalation to SIGKILL when the leader ignores SIGTERM.
    • A grandchild that ignores SIGTERM is killed after the leader exits (Linux only).
    • No signal after a normal exit.
    • Terminating twice is harmless.
  • cargo check -p openshell-sandbox passes.
  • mise run pre-commit was not run. cargo fmt, clippy, and tests were run directly instead.
  • Not exercised end to end against a gateway (mise run e2e). The tests drive ExecProcessGroup with the same spawn setup as ssh.rs. The russh spawn paths can't be unit-tested without a russh Handle.

Checklist

  • Follows Conventional Commits
  • Commits are signed off (DCO)
  • Architecture docs updated (if applicable)

…loses

When the gateway's exec deadline expires, or a caller cancels an exec
stream, the gateway closes the SSH channel. The supervisor did nothing to
the child, so the command kept running until it exited on its own or the
pod went away.

Each exec, shell, and sftp channel now runs its command as the leader of
its own process group. If the channel or the SSH connection closes before
the leader exits, the supervisor sends SIGTERM to the group, then SIGKILL
after a 5s grace period if any member is still running. The leader stays
unreaped until the escalation finishes, so a signal never reaches a
recycled PID or process group. Canonical main-session attachments are
unaffected.

Signed-off-by: Cameron Motevasselani <cameron@kindo.ai>
@link108
link108 merged commit d7763ea into main Oct 14, 2026
13 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants